﻿package com.wandian.util
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;

    public class RadioManage extends MovieClip
    {
        private var _arrRadio:Array;
        public var _radioId:int = 0;
        private var _isNull:Boolean;

        public function RadioManage(arrRadio:Array, isNull:Boolean = false) : void
        {
            var mcRadio:MovieClip = null;
            this._arrRadio = arrRadio;
            this._isNull = isNull;
            var index:int = 0;
            while (index < this._arrRadio.length)
            {
                
                mcRadio = this._arrRadio[index][1] as MovieClip;
                mcRadio.mouseChildren = false;
                mcRadio.buttonMode = true;
                mcRadio.id = this._arrRadio[index][0];
                mcRadio.addEventListener(MouseEvent.CLICK, this.clickRadioHandler);
                index = index + 1;
            }
            return;
        }

        public function setRadioSelect(id:int, boolEvent:Boolean = false) : void
        {
            var index:int = 0;
            while (index < this._arrRadio.length)
            {
                
                if (this._arrRadio[index][0] == id)
                {
                    this._arrRadio[index][1].gotoAndStop("yes");
                }
                else
                {
                    this._arrRadio[index][1].gotoAndStop("no");
                }
                index = index + 1;
            }
            this._radioId = id;
            if (!boolEvent)
            {
                dispatchEvent(new Event(Event.CHANGE));
            }
            return;
        }

        private function clickRadioHandler(event:MouseEvent) : void
        {
            var mcRadio:MovieClip = event.currentTarget as MovieClip;
            if (this._radioId == mcRadio.id)
            {
                if (this._isNull)
                {
                    mcRadio.gotoAndStop("no");
                    this._radioId = 0;
                    dispatchEvent(new Event(Event.CHANGE));
                }
                return;
            }
            this.setRadioSelect(mcRadio.id);
            return;
        }

    }
}
